from rest_framework.generics import ListAPIView, ListCreateAPIView
from rest_framework.permissions import IsAdminUser

from apps.meiduo_admin.serializers.user import UserModelSerializer
from apps.meiduo_admin.utils import PageNum
from apps.users.models import User


# meiduo_admin/users/?page=1&pagesize=10&keyword=
class UserListView(ListCreateAPIView):
    # 只允许管理员用户访问
    permission_classes = [IsAdminUser]
    # queryset查询集  返回所有用户
    queryset = User.objects.all()
    # 指定序列化器
    serializer_class = UserModelSerializer
    # 指定分页器
    pagination_class = PageNum

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')

        if keyword:
            return User.objects.filter(username__contains=keyword)

        return User.objects.all()


